import axios from "axios";
import store from "@/store/index"
import router from "@/router";
import { Indicator } from 'mint-ui';

export function api1(config) {
  return new Promise((resolve, reject) => {
    //请求前
    config.headers = config.headers || {}

    Indicator.open('加载中...');

    //判断是否传入headers:{token:true},那么则判断用户是否是登录状态
    if(config.headers.token){
      config.headers.token = store.state.user.token
      if(!config.headers.token){
        setTimeout(() =>{
          Indicator.close();
        },500)
        router.push('/login')
        return
      }
    }

    const instance = axios.create({
      baseURL: 'http://localhost:3000',
      timeout: 5000
    })
    //发生真正的网络请求
    instance(config).then(res => {
      let data = res.data.data;

      //请求完成
      setTimeout(() =>{
        Indicator.close();
      },500)

      resolve(data)
    }).catch(err => {
      reject(err)
    })

    //对请求前的拦截器
    instance.interceptors.request.use(
      config => {
        config.headers['Authorization'] = utilsAuth.getShopToken();
        return config
        // return config
      },

      error => {
        return Promise.reject(error)
      }
    )



  })







}